package com.gk.util.component
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 *复选框 
	 * @author Nestor
	 * 
	 */	
	public class CheckBox extends Component
	{
		protected var _back:Sprite;
		protected var _button:Sprite;
		protected var _label:Label;
		protected var _labelText:String = "";
		protected var _selected:Boolean = false;
		protected var _gap:int = 4;//文本和按钮的间隔
		
		
		/**
		 * Constructor
		 * @param parent The parent DisplayObjectContainer on which to add this CheckBox.
		 * @param xpos The x position to place this component.
		 * @param ypos The y position to place this component.
		 * @param label String containing the label for this component.
		 * @param defaultHandler The event handling function to handle the default event for this component (click in this case).
		 */
		public function CheckBox(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number =  0, label:String = "", defaultHandler:Function = null)
		{
			_labelText = label;
			super(parent, xpos, ypos);
			if(defaultHandler != null)
			{
				addEventListener(MouseEvent.CLICK, defaultHandler);
			}
		}
		
		/**
		 * Initializes the component.
		 */
		override protected function init():void
		{
			super.init();
			buttonMode = true;
			useHandCursor = true;
			mouseChildren = false;
		}
		
		/**
		 * Creates the children for this component
		 */
		override protected function addChildren():void
		{
			_back = new Sprite();
			_back.filters = [getShadow(2, true)];
			addChild(_back);
			
			_button = new Sprite();
			_button.filters = [getShadow(1)];
			_button.visible = false;
			addChild(_button);
			
			_label = new Label(this, 0, 0, _labelText);
			draw();
			
			addEventListener(MouseEvent.CLICK, onClick);
		}
		
		
		
		
		///////////////////////////////////
		// public methods
		///////////////////////////////////
		
		/**
		 * Draws the visual ui of the component.
		 */
		override public function draw():void
		{
			super.draw();
			_back.graphics.clear();
			_back.graphics.beginFill(0xCCCCCC);
			_back.graphics.drawRect(0, 0, 10, 10);
			_back.graphics.endFill();
			
			_button.graphics.clear();
			_button.graphics.beginFill(0xFFFFFF);
			_button.graphics.drawRect(0, 0, 6, 6);
			_button.graphics.endFill();
			
			_button.x = _back.width * 0.5 - _button.width * 0.5;
			_button.y = _back.height * 0.5 - _button.height * 0.5;
			
			_label.text = _labelText;
			_label.draw();
			_label.x = _back.width + gap;
			_label.y = (_back.height - _label.height) / 2;
			setSize(_label.width + _label.x, _back.height);
		}
		
		
		/**
		 *设置复选框 背景 
		 * @param dobj  null 为恢复默认样式
		 * 
		 */		
		public function setBack(dobj:DisplayObject):void{
			while(_back.numChildren>0){
				_back.removeChildAt(0);
			}
			if(dobj){
				_back.addChild(dobj);
			}
		}
		/**
		 *设置复选框 按钮
		 * @param dobj null 为恢复默认样式
		 * 
		 */	
		public function setBtn(dobj:DisplayObject):void{
			while(_button.numChildren>0){
				_button.removeChildAt(0);
			}
			if(dobj){
				_button.addChild(dobj);
			}
		}
		
		
		///////////////////////////////////
		// event handler
		///////////////////////////////////
		
		/**
		 * Internal click handler.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onClick(event:MouseEvent):void
		{
			_selected = !_selected;
			_button.visible = _selected;
		}
		
		
		
		
		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		
		/**
		 * Sets / gets the label text shown on this CheckBox.
		 */
		public function set label(str:String):void
		{
			_labelText = str;
			invalidate();
		}
		public function get label():String
		{
			return _labelText;
		}
		
		/**
		 * Sets / gets the selected state of this CheckBox.
		 */
		public function set selected(s:Boolean):void
		{
			_selected = s;
			_button.visible = _selected;
		}
		public function get selected():Boolean
		{
			return _selected;
		}

		/**
		 * Sets/gets whether this component will be enabled or not.
		 */
		public override function set enabled(value:Boolean):void
		{
			super.enabled = value;
			mouseChildren = false;
		}

		public function get gap():int
		{
			return _gap;
		}
		/**
		 * 文本和按钮的间隔
		 * @param value
		 * 
		 */
		public function set gap(value:int):void
		{
			_gap = value;
		}

	}
}